package core;

import java.util.Calendar;
import java.util.Date;

public class PriceData {

	public float open;
	public float close;
	public float high;
	public float low;

	public Date time;

	public PriceData() {
		open = 0;
		close = 0;
		high = 0;
		low = 0;
		time = null;
	}

	public PriceData(PriceData another) {
		open = another.open;
		close = another.close;
		high = another.high;
		low = another.low;
		time = another.time;
	}

	public PriceData(String text) {
		String data[] = text.split(",");
		if (data.length != 8) {
			System.err.println("Error Data length : " + text);
		} else {
			try {
				Calendar allDate = Calendar.getInstance();
				int year = Integer.parseInt(data[1].substring(0, 4));
				int month = Integer.parseInt(data[1].substring(4, 6));
				int date = Integer.parseInt(data[1].substring(6));
				int hrs = Integer.parseInt(data[2].substring(0, 2));
				int min = Integer.parseInt(data[2].substring(2, 4));
				int sec = Integer.parseInt(data[2].substring(4));
				allDate.set(Calendar.YEAR, year);
				allDate.set(Calendar.MONTH, month);
				allDate.set(Calendar.DATE, date);
				allDate.set(Calendar.HOUR, hrs);
				allDate.set(Calendar.MINUTE, min);
				allDate.set(Calendar.SECOND, sec);

				time = allDate.getTime(); // ������ת��Ϊdate

				// SimpleDateFormat df = new SimpleDateFormat(
				// "yyyy-MM-dd hh:mm:ss"); // ��date�������ڸ�ʽ��
				// String sss = df.format(time);
				//
				// System.out.println("Date : " + sss);

				open = Float.parseFloat(data[3]);
				high = Float.parseFloat(data[4]);
				low = Float.parseFloat(data[5]);
				close = Float.parseFloat(data[6]);

			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}

	public static PriceData add(PriceData one, PriceData another) {
		PriceData result = new PriceData(one);

		result.open += another.open;
		result.high += another.high;
		result.low += another.low;
		result.close += another.close;

		return result;
	}

	public static PriceData sub(PriceData one, PriceData another) {
		PriceData result = new PriceData(one);

		result.open -= another.open;
		result.high -= another.high;
		result.low -= another.low;
		result.close -= another.close;

		return result;
	}

	public static PriceData div(PriceData price, float number) {
		PriceData result = null;
		if (number < 0.01f) {
			System.err.println("number of div is too small!");
		} else {
			result = new PriceData();
			result.open = price.open / number;
			result.high = price.high / number;
			result.low = price.low / number;
			result.close = price.close / number;
		}

		return result;
	}
}
